/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ufpr.ees.suhck.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperRunManager;
import ufpr.ees.suhck.model.Relatorio;

/**
 *
 * @author Cassio
 */
public class FrontRelatorio extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        

        String query = "";
        String relatorio = request.getParameter("action");
        String data_inicial = "";
        String data_final = "";
        String cpf = "";
        String filtro = "";
        Relatorio rel = new Relatorio();
        
        try {
            switch (relatorio) {
                case "RelatorioAgendamento":
                    data_inicial = request.getParameter("data1") + " " + request.getParameter("hora1") + ":00";
                    data_final = request.getParameter("data2") + " " + request.getParameter("hora2") + ":00";
                    relatorio = "agendamento";
                    query = rel.agendamento(data_inicial, data_final);
                    filtro = data_inicial + " " + data_final;
                    break;
                case "RelatorioAtendimento":
                    data_inicial = request.getParameter("data3") + " " + request.getParameter("hora3") + ":00";
                    data_final = request.getParameter("data4") + " " + request.getParameter("hora4") + ":00";
                    relatorio = "atendimento";
                    query = rel.atendimento(data_inicial, data_final);
                    filtro = data_inicial + " " + data_final;
                    break;
                case "RelatorioCliente":
                    cpf = request.getParameter("cpfatendente");
                    query = rel.cliente(cpf);
                    relatorio = "cliente";
                    filtro = cpf;
                    break;
            }

            System.out.println("CPF: " + cpf);
            System.out.println("Data inicio: " + data_inicial);
            System.out.println("Data final: " + data_final);
            System.out.println("Query: " + query);
            System.out.println("Relatorio: " + relatorio);

            HashMap hashMap = new HashMap();
            hashMap.put("query", query);
            hashMap.put("filtro", filtro);
            hashMap.put(JRParameter.REPORT_LOCALE, new Locale("pt", "BR"));

            String path = "WEB-INF/relatorios/" + relatorio + ".jasper";
            File reportFile = new File(getServletConfig().getServletContext().getRealPath(path));
            System.out.println(path);

            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), hashMap, rel.getConexao());
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "pdf; filename=Relatorio" + new Date().getTime() + ".pdf");

            response.setContentLength(bytes.length);

            ServletOutputStream servletOutputStream;
            servletOutputStream = response.getOutputStream();
            servletOutputStream.write(bytes, 0, bytes.length);
            servletOutputStream.flush();
            servletOutputStream.close();

        } catch (JRException | IOException ex) {
            Logger.getLogger(FrontRelatorio.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //db.desconectar();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
